home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / oop55.zip / FIGURES.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  5KB  |  201 lines

  1.  
  2. { Turbo Figures }
  3. { Copyright (c) 1989 by Borland Interational, Inc. }
  4.  
  5. unit Figures;
  6. { From P-42 of the Object-Oriented Programming Guide.
  7.   Virtual methods & polymorphic objects.
  8. }
  9.  
  10. interface
  11.  
  12. uses Graph, Crt;
  13.  
  14. type
  15.   Location = object
  16.     X,Y : Integer;
  17.     procedure Init(InitX, InitY : Integer);
  18.     function GetX : Integer;
  19.     function GetY : Integer;
  20.   end;
  21.  
  22.   PointPtr = ^Point;
  23.  
  24.   Point    = object (Location)
  25.     Visible : Boolean;
  26.     constructor Init(InitX, InitY : Integer);
  27.     destructor Done; virtual;
  28.     procedure Show; virtual;
  29.     procedure Hide; virtual;
  30.     function IsVisible : Boolean;
  31.     procedure MoveTo(NewX, NewY : Integer);
  32.     procedure Drag(DragBy : Integer); virtual;
  33.   end;
  34.  
  35.   CirclePtr = ^Circle;
  36.  
  37.   Circle   = object (Point)
  38.     Radius : Integer;
  39.     constructor Init(InitX, InitY : Integer;
  40.                      InitRadius : Integer);
  41.     procedure Show; virtual;
  42.     procedure Hide; virtual;
  43.     procedure Expand(ExpandBy : Integer); virtual;
  44.     procedure Contract(ContractBy : Integer); virtual;
  45.   end;
  46.  
  47. implementation
  48.  
  49. {--------------------------------------------------------}
  50. { Location's method implementations:                     }
  51. {--------------------------------------------------------}
  52.  
  53. procedure Location.Init(InitX, InitY : Integer);
  54.  
  55. begin
  56.   X := InitX;
  57.   Y := InitY;
  58. end;
  59.  
  60. function Location.GetX : Integer;
  61. begin
  62.   GetX := X;
  63. end;
  64.  
  65. function Location.GetY : Integer;
  66. begin
  67.   GetY := Y;
  68. end;
  69.  
  70.  
  71. {--------------------------------------------------------}
  72. { Points's method implementations:                       }
  73. {--------------------------------------------------------}
  74.  
  75. constructor Point.Init(InitX, InitY : Integer);
  76. begin
  77.   Location.Init(InitX, InitY);
  78.   Visible := False;
  79. end;
  80.  
  81. destructor Point.Done;
  82. begin
  83.   Hide;
  84. end;
  85.  
  86. procedure Point.Show;
  87. begin
  88.   Visible := True;
  89.   PutPixel(X, Y, GetColor);
  90. end;
  91.  
  92. procedure Point.Hide;
  93. begin
  94.   Visible := False;
  95.   PutPixel(X, Y, GetBkColor);
  96. end;
  97.  
  98. function Point.IsVisible : Boolean;
  99. begin
  100.   IsVisible := Visible;
  101. end;
  102.  
  103. procedure Point.MoveTo(NewX, NewY : Integer);
  104. begin
  105.   Hide;
  106.   X := NewX;
  107.   Y := NewY;
  108.   Show;
  109. end;
  110.  
  111. function GetDelta(var DeltaX : Integer;
  112.                   var DeltaY : Integer) : Boolean;
  113. var
  114.   KeyChar : Char;
  115.   Quit : Boolean;
  116. begin
  117.   DeltaX := 0; DeltaY := 0;  { 0 means no change in position;  }
  118.   GetDelta := True;          { True means we return a delta    }
  119.   repeat
  120.     KeyChar := ReadKey;      { First, read the keystroke }
  121.     Quit := True;            { Assume it's a useable key }
  122.     case Ord(KeyChar) of
  123.        0: begin          { 0 means an extended, 2-byte code }
  124.             KeyChar := ReadKey;  { Read second byte of code }
  125.             case Ord(KeyChar) of
  126.              72: DeltaY := -1;   { Up arrow; decrement Y }
  127.              80: DeltaY := 1;    { Down arrow; increment Y }
  128.              75: DeltaX := -1;   { Left arrow; decrement X }
  129.              77: DeltaX := 1;    { Right arrow; increment X }
  130.              else Quit := False; { Ignore any other code }
  131.             end; { case }
  132.           end;
  133.       13: GetDelta := False; { CR pressed means no delta  }
  134.       else Quit := False;    { Ignore any other keystroke }
  135.     end;  { case }
  136.   until Quit;
  137. end;
  138.  
  139. procedure Point.Drag(DragBy : Integer);
  140. var
  141.   DeltaX, DeltaY : Integer;
  142.   FigureX, FigureY : Integer;
  143. begin
  144.   Show;               { Display figure to be dragged }
  145.   FigureX := GetX;    { Get the initial position of figure }
  146.   FigureY := GetY;
  147.  
  148.   { This is the drag loop : }
  149.   while GetDelta(DeltaX, DeltaY) do
  150.   begin               { Apply delta to figure X,Y: }
  151.     FigureX := FigureX + (DeltaX * DragBy);
  152.     FigureY := FigureY + (DeltaY * DragBy);
  153.     MoveTo(FigureX, FigureY); { And tell the figure to move }
  154.   end;
  155. end;
  156.  
  157. {--------------------------------------------------------}
  158. { Circle's method implementations:                       }
  159. {--------------------------------------------------------}
  160.  
  161. constructor Circle.Init(InitX, InitY : Integer;
  162.                         InitRadius : Integer);
  163. begin
  164.   Point.Init(InitX, InitY);
  165.   Radius := InitRadius;
  166. end;
  167.  
  168. procedure Circle.Show;
  169. begin
  170.   Visible := True;
  171.   Graph.Circle(X, Y, Radius);
  172. end;
  173.  
  174. procedure Circle.Hide;
  175. var
  176.   TempColor : Word;
  177. begin
  178.   TempColor := Graph.GetColor;
  179.   Graph.SetColor(GetBkColor);
  180.   Visible := False;
  181.   Graph.Circle(X, Y, Radius);
  182.   Graph.SetColor(TempColor);
  183. end;
  184.  
  185. procedure Circle.Expand(ExpandBy : Integer);
  186. begin
  187.   Hide;
  188.   Radius := Radius + ExpandBy;
  189.   if Radius <0 then Radius := 0;
  190.   Show;
  191. end;
  192.  
  193. procedure Circle.Contract(ContractBy : Integer);
  194. begin
  195.   Expand(-ContractBy);
  196. end;
  197.  
  198. { No initialization section }
  199.  
  200. end.
  201.